<template>
    <el-card class="mb-20" shadow="hover">
        <template #header>
            <span>仓库列表</span>
        </template>
        <el-form :model="searchForm" inline>
            <el-form-item label="仓库名称">
                <el-input v-model="searchForm.searchRepositoryName" placeholder="请输入仓库名称"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="searchWarehouse">搜索</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="warehouseList" stripe border style="width: 100%">
            <el-table-column prop="id" label="仓库ID"></el-table-column>
            <el-table-column prop="repositoryName" label="仓库名称"></el-table-column>
            <el-table-column prop="repositoryAddress" label="仓库地址"></el-table-column>
            <el-table-column prop="upperLimit" label="仓库容量"></el-table-column>
            <el-table-column label="操作" width="300">
                <template #default="scope">
                    <el-button
                        type="primary"
                        icon="el-icon-view"
                        size="mini"
                        @click="viewProducts(scope.row.id)">查看商品</el-button>
                    <el-button
                        type="primary"
                        icon="el-icon-edit"
                        size="mini"
                        @click="editWarehouse(scope.row.id)">编辑</el-button>
                    <el-button
                        type="danger"
                        icon="el-icon-delete"
                        size="mini"
                        @click="deleteWarehouse(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="current"
            :page-sizes="pageList"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
        ></el-pagination>

        <!-- 编辑仓库对话框 -->
        <el-dialog title="编辑仓库" :visible.sync="editDialogVisible" width="50%">
            <el-form :model="editForm" label-width="120px">
                <el-form-item label="仓库名称">
                    <el-input v-model="editForm.repositoryName"></el-input>
                </el-form-item>
                <el-form-item label="仓库地址">
                    <el-input v-model="editForm.repositoryAddress"></el-input>
                </el-form-item>
                <el-form-item label="库存上限">
                    <el-input-number v-model="editForm.upperLimit" :min="0"></el-input-number>
                </el-form-item>
                <el-form-item label="库存下限">
                    <el-input-number v-model="editForm.lowerLimit" :min="0" :max="editForm.upperLimit"></el-input-number>
                </el-form-item>
                <el-form-item label="最大存储量">
                    <el-input-number v-model="editForm.maximumStorage" :min="0"></el-input-number>
                </el-form-item>
                <el-form-item label="仓库描述">
                    <el-input type="textarea" v-model="editForm.content"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="confirmEdit">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 查看商品对话框 -->
        <el-dialog title="仓库商品列表" :visible.sync="productDialogVisible" width="70%">
            <el-table :data="productList" border style="width: 100%">
                <el-table-column prop="productId" label="商品ID" width="120"></el-table-column>
                <el-table-column label="商品名称" >
                    <template slot-scope="scope">
                        {{scope.row.products[0].name}}
                    </template>
                </el-table-column>
                <el-table-column prop="count" label="库存数量" width="120"></el-table-column>
            </el-table>
            <span slot="footer" class="dialog-footer">
                <el-button @click="productDialogVisible = false">关 闭</el-button>
            </span>
        </el-dialog>
    </el-card>
</template>

<script>
export default {
    data() {
        return {
            searchForm: {
                searchRepositoryName: ''
            },
            warehouseList: [], // 仓库列表
            total: 0,
            current: 1,
            pageSize: 10,
            pageList: [5, 10, 15, 20],
            vo: {},

            // 编辑相关数据
            editDialogVisible: false,
            editForm: {
                id: '',
                repositoryName: '',
                repositoryAddress: '',
                upperLimit: 100,
                lowerLimit: 10,
                maximumStorage: 1000,
                content: ''
            },
            currentEditId: null,

            // 商品查看相关数据
            productDialogVisible: false,
            productList: [],
            currentRepositoryId: null
        };
    },
    methods: {
        // 显示仓库的数据 - 保持原样
        fetchWarehouseList() {
            this.$axios.post(`repository/selectRepository/${this.current}/${this.pageSize}`, this.vo).then(r => {
                this.warehouseList = r.data.list
                this.total = r.data.total
            })
        },

        // 查看仓库商品
        viewProducts(repositoryId) {
            this.currentRepositoryId = repositoryId;
            this.$axios.post(`repositoryCommodity/selectRepositoryProducts/${repositoryId}`)
                .then(response => {
                    console.log(999,response.data)
                    this.productList = response.data;
                    this.productDialogVisible = true;
                })
                .catch(error => {
                    this.$message.error('获取商品列表失败: ' + error.message);
                });
        },

        // 修改 - 保持方法签名不变，但完善内部实现
        editWarehouse(id) {
            this.currentEditId = id;
            // 获取仓库详情
            this.$axios.post(`repository/getRepositoryById/${id}`).then(response => {
                this.editForm = {
                    id: response.data.id,
                    repositoryName: response.data.repositoryName,
                    repositoryAddress: response.data.repositoryAddress,
                    upperLimit: response.data.upperLimit,
                    lowerLimit: response.data.lowerLimit,
                    maximumStorage: response.data.maximumStorage,
                    content: response.data.content || ''
                };
                this.editDialogVisible = true;
            }).catch(error => {
                this.$message.error('获取仓库信息失败: ' + error.message);
            });
        },

        // 确认编辑
        confirmEdit() {
            this.$axios.post(`repository/updateRepository`, this.editForm)
                .then(() => {
                    this.$message.success('仓库信息更新成功');
                    this.editDialogVisible = false;
                    this.fetchWarehouseList(); // 刷新列表
                })
                .catch(error => {
                    this.$message.error('更新失败: ' + error.message);
                });
        },

        // 删除仓库 - 保持方法签名不变，但完善内部实现
        deleteWarehouse(id) {
            this.$confirm('确定要删除这个仓库吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$axios.delete(`repository/deleteRepository/${id}`)
                    .then(() => {
                        this.$message.success('删除成功');
                        this.fetchWarehouseList(); // 刷新列表
                    })
                    .catch(error => {
                        this.$message.error('删除失败: ' + error.message);
                    });
            }).catch(() => {
                this.$message.info('已取消删除');
            });
        },

        // 页大小 - 保持原样
        handleSizeChange(newSize) {
            this.pageSize = newSize;
            this.fetchWarehouseList();
        },

        // 页号 - 保持原样
        handleCurrentChange(newPage) {
            this.current = newPage;
            this.fetchWarehouseList();
        },

        // 搜索 - 保持原样
        searchWarehouse() {
            this.vo = this.searchForm
            this.fetchWarehouseList();
        }
    },

    mounted() {
        this.fetchWarehouseList();
    }
};
</script>

<style scoped>
.mb-20 {
    margin-bottom: 20px;
}

/* 调整按钮间距 */
.el-button + .el-button {
    margin-left: 8px;
}
</style>